import axios from 'axios';
import router from '../router/index'
import { MessageBox, Message } from 'element-ui';

const service = axios.create({
    // process.env.NODE_ENV === 'development' 来判断是否开发环境
    // easy-mock服务挂了，暂时不使用了
    // baseURL: '/api',
	baseURL:'/api/index.php/Home/Index',
    timeout: 5000
});

service.interceptors.request.use(
    config => {
    	var token = sessionStorage.getItem("token")
    	if(token){
    		config.headers.Authtoken = token
    	}else{
    		router.push('/login')
    	}
    	
        return config;
    },
    error => {
        console.log(error);
        return Promise.reject();
    }
);

service.interceptors.response.use(
    response => {
        if (response.status === 200) {
           if(response.data.code === 401){
           	Message.error(response.data.message);
           	return router.push('/login')
           }else{
           	return response.data;
           }
        } else if(response.status === 500){
            return router.push('/login')
        } else {
            Promise.reject();
        }
    },
    error => {
        router.push('/login')
        return Promise.reject();
    }
);

export default service;
